package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectPageVo;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author ffk
 * @date 2024/5/9 15:53
 */

@Service
public class NursingProjectServiceImpl implements NursingProjectService {


    @Autowired
    NursingProjectMapper nursingProjectMapper;

    //分页查询
    @Override
    public PageResponse<NursingProjectPageVo> findByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum,pageSize);

      Page<NursingProject> page=nursingProjectMapper.getPageList(name,status);//查的是这张表，所有用它对应的实体类NursingProject
        PageResponse<NursingProjectPageVo> of = PageResponse.of(page, NursingProjectPageVo.class);

        return of;

    }

    //添加项目
    @Override
    public void addOneProject(NursingProjectDto nursingProjectDto) {
        nursingProjectDto.setCreateTime(LocalDateTime.now());
        nursingProjectDto.setCreateBy(1L);
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.addTheProject(nursingProject);
    }


    //通过id查询项目
    @Override
    public NursingProjectPageVo queryOneById(Long id) {

        NursingProject nursingProject=nursingProjectMapper.queryTheById(id);
        NursingProjectPageVo nursingProjectPageVo = BeanUtil.toBean(nursingProject, NursingProjectPageVo.class);

        return nursingProjectPageVo;
    }

    //修改项目
    @Override
    public void updateOneProject(NursingProjectDto nursingProjectDto) {
        nursingProjectDto.setUpdateBy(1L);
        nursingProjectDto.setUpdateTime(LocalDateTime.now());

        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);

        nursingProjectMapper.updateOne(nursingProject);
    }

    //删除项目
    @Override
    public void deleteOneProject(Long id) {
        nursingProjectMapper.deleteOne(id);
    }

    //修改项目状态
    @Override
    public void updateProjectStatus(Long id, Integer status) {
        nursingProjectMapper.updateProjectStatus(id,status);
    }
}
